var currentHostUrl =  window.location.protocol + "//" + window.location.host + "/";
// var apiHostUrl = "http://127.0.0.1:8020/"
var apiHostUrl = "http://api.junxtec.com/"

var softVersion = "MS4wLjE=";
var softVersionId = "MjAxODAyMTM=";

var token = $.cookie("token");
var userId = $.cookie("userId");

$(document).ready(function() {
    //JavaScript代码区域
    layui.use(['element', 'layer'], function() {
        var element = layui.element;
        var layer = layui.layer;

        getAllNavMenu(layer, element);

    });
    loadingPage("main");
});

function loadingPage(url) {
    // 加载后台的子页面
    $("#ajax-content").load(url + ".html", function(response, status, xhr) {
        if (status === "error") {
            $(this).html(response);
        }
    });
}

function loadingChildPage(url, title) {
    // 加载后台的子页面的子页面
    $("#page-right-content-ajax").load(url + ".html", function(response, status, xhr) {
        // 变更网站地图
        changeWebSiteMap(title);
        // 隐藏工具条
        $('#page-toolbar-content').hide();
        // 显示子页面通知
        $('#page-toolbar-title').show();
        if (status === "error") {
            $(this).html(response);
        }
    });
}

function changeWebSiteMap(title) {
    var map = $('#website-map-title');
    map.empty();
    map.html(title);
}

/**
 * 获取所有的导航菜单
 */
function getAllNavMenu(layer, element) {

    // 获取所有的导航菜单
    $.ajax({
        type: "POST",
        datatype: "json",
        async: false,
        url: "../json/testNavMenuData.json",
        success: function (returnData) {
            if (returnData.errorCode === 0) {
                createNavMenu(returnData.returnJson, element);
            }
            else {
                layer.alert("获取菜单失败!");
            }
        }
    });

}

/**
 * 获取每个页面的按钮
 */
function getAllButton(layer) {

    var menu_id;

    // 获取该页面所有的按钮
    $.ajax({
        type: "POST",
        datatype: "json",
        async: false,
        data: JSON.stringify({
            "menuId": menu_id,
        }),
        url: "../json/testNavMenuData.json",
        success: function (returnData) {
            if (returnData.errorCode === 0) {
                createButton(returnData.returnJson);
            }
            else {
                layer.alert("获取按钮失败!");
            }
        }
    });

}

/**
 * 动态生成导航菜单
 * @param data
 */
function createNavMenu(data, element) {

    var menu_array = new Array();
    var childen_menu_array = new Array();

    for (var i = 0; i < data.length; i++) {
        // 遍历顶级菜单
        if (data[i].menuParentId === "#") {
            menu_array.push(data[i]);
        }
        // 遍历子菜单
        else {
            childen_menu_array.push(data[i]);
        }
    }

    var menu_content = $('#nav-menu-content');

    // 遍历所有的顶级菜单
    for (var i = 0; i < menu_array.length; i++) {

        // 如果是首页菜单
        if (i === 0) {
            menu_content.append(
                "<li class='layui-nav-item layui-nav-itemed layui-this'>" +
                "<a id='nav-menu-home' href='javascript:loadingPage(\"" + menu_array[0].menuUrl + "\");'>" +
                "<b>WMS管理平台</b>" +
                "</a></li>"
            );
        }
        // 如果是非首页菜单
        else {

            var menu = menu_array[i];

            // 如果是菜单组
            if (menu.menuType === 0) {
                menu_content.append(
                    "<li class='layui-nav-item layui-nav-itemed'>" +
                    "<a href='javascript:;'>" +
                    "<i class='fa " + menu.menuIcon + " fa-fw nav-menu-group'></i>" +
                    menu.menuName + "</a>" +
                    "<dl class='layui-nav-child' id='dl_" + i + "'></dl></li>"
                );

                // 为该顶级菜单加载子菜单
                for (var j = 0; j < childen_menu_array.length; j++) {

                    var childenMenu = childen_menu_array[j];

                    // 如果该子菜单不是菜单组，并且子菜单的父级菜单id 等于 该顶级菜单的id
                    // 说明该子菜单 归属于 该顶级菜单
                    if (childenMenu.menuType !== 0 && childenMenu.menuParentId === (menu.id.toString())) {

                        // 动态添加该子菜单到该顶级菜单下
                        $('#dl_' + i).append(
                            "<dd>" +
                            "<a href='javascript:loadingPage(\"" + childenMenu.menuUrl + "\");'>" +
                            "<i class='fa " + childenMenu.menuIcon + " fa-fw nav-menu-page'></i>" +
                            childenMenu.menuName + "</a>" +
                            "</dd>"
                        );

                    }
                }

            }
            else {
                menu_content.append(
                    "<li class='layui-nav-item layui-nav-itemed'>" +
                    "<a href='javascript:loadingPage(\"" + menu.menuUrl + "\");'>" +
                    "<i class='fa " + menu.menuIcon + " fa-fw nav-menu-group'></i>" +
                    menu.menuName + "</a></li>"
                );
            }

        }
    }

    // 重新渲染导航
    element.render('nav');

}

function createButton(data) {

    var button_content = $(".menu-btn-group");
    button_content.empty();

    var button_array = new Array();

    // 将按钮数据添加进容器
    for (var i = 0; i < data.length; i++) {
        if (data[i].menuType === 2) {
            button_array.push(data[i]);
        }
    }

    // 遍历所有的按钮数据
    for (var i = 0; i < button_array.length; i++) {

        var button = button_array[i];

        // 动态生成按钮
        button_content.append(
            "<button class='layui-btn " + button.buttonStyle + "' data-type='" + button.buttonDataType + "'>" +
            "<i class='fa " + button.menuIcon + " fa-fw'></i>" +
            button.menuName + "</button>"
        );
    }

}
